package com.lucio.mybatis.core;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

public class Resources {

    public static Reader getResourceAsReader(String sourcePath) throws IOException {
        return new InputStreamReader(getResourceAsStream(sourcePath));
    }

    public static InputStream getResourceAsStream(String sourcePath) throws IOException {
        ClassLoader[] classLoaders = getClassLoaders();
        for (ClassLoader classLoader : classLoaders) {
            InputStream resourceAsStream = classLoader.getResourceAsStream(sourcePath);
            if (null != resourceAsStream) {
                return resourceAsStream;
            }
        }
        throw new IOException("can't find resource: " + sourcePath);
    }

    public static ClassLoader[] getClassLoaders() {
        return new ClassLoader[]{ClassLoader.getSystemClassLoader(), Thread.currentThread().getContextClassLoader()};
    }
}
